package com.liu.sunnyday.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.liu.sunnyday.constant.MessageConstant;
import com.liu.sunnyday.entity.PageResult;
import com.liu.sunnyday.entity.QueryPageBean;
import com.liu.sunnyday.entity.Result;
import com.liu.sunnyday.pojo.CheckItem;
import com.liu.sunnyday.service.CheckItemService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * chance！Time
 * ：[体检检查项]
 *
 * @author liuqian
 * @date 2022-06-08 12:17
 * *
 */
@RestController
@RequestMapping("/checkitem")
public class CheckItemController {
    //调用远程服务
    @Reference
    private CheckItemService checkItemService;

    /**
     * 1A查询所有检查项
     * B查询所有检查项（新增检查组中需要）
     * @return
     */
    @GetMapping("/findAll")
    public Result findAll() {
        //封装返回的结果
        //调用服务查询所有检查项
        List<CheckItem> list = checkItemService.findAll();
        return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, list);
    }

    /*2新增体检检查项，点击确定 */
    @RequestMapping("/add")
    public Result add(@RequestBody CheckItem checkItem) {
        checkItemService.add(checkItem);
        return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
    }

    /*3分页查询所有检查项*/
    @PostMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {
    //调用服务分页查询
    //System.out.println(queryPageBean.getQueryString());
        PageInfo<CheckItem> pageInfo = checkItemService.findPage(queryPageBean);
        PageResult pageResult = new PageResult(pageInfo.getTotal(), pageInfo.getList());

    //System.out.println(pageInfo);
        return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, pageResult);
    }

    //4-删除
    @PostMapping("/deleteById")
    public Result deleteById(Integer id) {
        checkItemService.deleteById(id);
        return new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
    }

    //5-编辑检查项时，弹出表单的数据回显需要根据id去查询
    @GetMapping("/findById")
    public Result findById(Integer id) {
        CheckItem checkItem = checkItemService.findById(id);
        return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, checkItem);
    }
    //5-提交表单进行修改
    @PostMapping("/updata")
    public Result updata(@RequestBody CheckItem checkItem) {
        checkItemService.updata(checkItem);
        return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
    }


}
